package com.daguchuangyi.erp_android.productlist.imagetell;

import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.daguchuangyi.erp_android.R;
import com.daguchuangyi.erp_android.util.ImageUtil;
import com.luck.picture.lib.entity.LocalMedia;

import java.util.List;

/**
 * Created by lijianjian on 2018/3/11.
 */

public class GalleryPenddingAdapter extends BaseQuickAdapter<LocalMedia, BaseViewHolder> {

    private int mCheckedPosition = 0;
    private OnLocalMediaCheckListener mOnLocalMediaCheckListener;

    public void setOnLocalMediaCheckListener(OnLocalMediaCheckListener onLocalMediaCheckListener) {
        mOnLocalMediaCheckListener = onLocalMediaCheckListener;
    }

    public GalleryPenddingAdapter(@Nullable List<LocalMedia> data) {
        super(R.layout.item_gallery_pendding_layout, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, LocalMedia item) {
        if (helper.getAdapterPosition() == mCheckedPosition) {
            helper.getView(R.id.check_hint_iv).setVisibility(View.VISIBLE);
        } else {
            helper.getView(R.id.check_hint_iv).setVisibility(View.GONE);
        }
        ImageView imageView = helper.getView(R.id.image_iv);
        String path = TextUtils.isEmpty(item.getCutPath()) ? item.getPath() : item.getCutPath();
        Glide.with(helper.itemView.getContext())
                .load(path)
                .apply(ImageUtil.getGlideRequestOptions())
                .into(imageView);

        helper.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int selfPosition = helper.getAdapterPosition();
                if (selfPosition != mCheckedPosition) {
                    int oldPosition = mCheckedPosition;
                    mCheckedPosition = selfPosition;
                    notifyItemChanged(oldPosition);
                    helper.getView(R.id.check_hint_iv).setVisibility(View.VISIBLE);

                    if (mOnLocalMediaCheckListener != null) {
                        mOnLocalMediaCheckListener.onPictureCheck(item);
                    }
                }
            }
        });

    }

    public interface OnLocalMediaCheckListener {
        public void onPictureCheck(LocalMedia localMedia);
    }
}
